package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.controller.form.*;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ParameterException;
import com.woniuxy.model.Member;
import com.woniuxy.service.MemberService;
import com.woniuxy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hujianying
 * @since 2022-03-28
 */
@Api(tags = "会员相关接口")
@RestController
@RequestMapping("/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    /**
     * 分页查询所有会员
     * @param findAllMenberform
     * @return
     */
    @ApiOperation("查会员")
    @GetMapping("getAllMember")
    public Result getMenberpage(FindAllMenberform findAllMenberform){
        return memberService.findMemberpage(findAllMenberform);
    }

    /**
     * 模糊分页查询所有
     * @param findAllMenberbyLikeform
     * @return
     */
    @ApiOperation("模糊查询会员")
    @GetMapping("/findMemberbylike")
    public Result getMenberpagebyLike(FindAllMenberbyLikeform findAllMenberbyLikeform){


        return memberService.findMemberpagebylike(findAllMenberbyLikeform);
    }

    /**
     * 删除会员对象
     * @param deleMenberforom
     * @return
     * @throws DdException
     */
    @ApiOperation("删除询会员")

    @PostMapping("/delememberById")
    public Result deleMenberpage(@RequestBody DeleMenberforom deleMenberforom) throws DdException {
        return memberService.deleMenbers(deleMenberforom);
    }

    /**
     * 新增会员对象 较为复杂
     * 先做参数校验
     * @param addMenberform
     * @param bindingResult
     * @return
     * @throws DdException
     */
    @ApiOperation("新增询会员")
    @PostMapping("/addmember")
    public Result addMember(@RequestBody @Valid AddMenberform addMenberform,
                            BindingResult bindingResult) throws DdException {
        if (bindingResult.hasErrors()) {
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            String s="";
            for (ObjectError allError : allErrors) {
                s = allError.getDefaultMessage();
            }
            throw new ParameterException(500003, s);
        }


        return memberService.addMember(addMenberform);
    }

    /**
     * 更新会员
     * @param upadateMemberform
     * @return
     */
    @PostMapping("/updateMember")
    public Result updateMember(@RequestBody UpadateMemberform upadateMemberform){
            memberService.update(upadateMemberform.getMember(),new QueryWrapper<Member>().eq("member_id",upadateMemberform.getMember().getMemberId()));
        return Result.success(200,"修改成功",null);
    }

}

